package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xgkj.diyiketang.data.PhotoInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.util.zoom.PhotoView;
import com.xgkj.diyiketang.util.zoom.PhotoViewAttacher;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.util.zoom.ViewPagerFixed;

import java.util.ArrayList;

/**
 * Created by XCKJ_Paladowow on 2016/5/9.
 */
public class ShowBigImageActivity extends BaseActivity {
    private Context mContext;
    private PhotoView mPhotoView;
    private String mBigImage;
//    private TextView back;
    private PhotoViewAttacher viewAttacher;
    private ViewPagerFixed viewPagerFixed;

    private GalleryAdapter mAdapter;
    private ArrayList<String> photoList = new ArrayList<String>();
    private int currentPosition;
    private ViewUtil.Size size;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showimage);
        mContext = this;
        size = new ViewUtil.Size(ViewUtil.getScreenWidth(mContext),0);
        initView();
    }
    private void initView(){
//        back = (TextView) findViewById(R.id.back);
//        back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
        Intent intent = getIntent();
        photoList = intent.getStringArrayListExtra("photoList");
        currentPosition = intent.getIntExtra("currentPosition",0);
        if (photoList == null || photoList.size()==0){
            finish();
            return;
        }
//        mBigImage = intent.getStringExtra("BIGIMAGE");
//        mPhotoView = (PhotoView) findViewById(R.id.image_big);
//        mPhotoView.setImageResource(R.drawable.zhanweitu);
//        viewAttacher = new PhotoViewAttacher(mPhotoView);
//        viewAttacher.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
//            @Override
//            public void onViewTap(View view, float x, float y) {
//                finish();
//            }
//        });
//       // mPhotoView.setImageBitmap();
//        ImageCache.getInstance(mContext).loadBitmapToView(mPhotoView, mBigImage, null);

        //viewpage
        viewPagerFixed = (ViewPagerFixed) findViewById(R.id.viewPager);
        mAdapter = new GalleryAdapter();
        viewPagerFixed.setAdapter(mAdapter);
        viewPagerFixed.setCurrentItem(currentPosition);

    }


    class GalleryAdapter extends PagerAdapter {
        public GalleryAdapter() {
        }

        @Override
        public int getCount() {
            return photoList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            String info = photoList.get(position);
            PhotoView img = new PhotoView(mContext);
            img.setTag(info);
            ImageCache.getInstance(mContext).loadBitmapToView(img,info,size);
            img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            container.addView(img);
            return img;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            String info = photoList.get(position);
            PhotoView image = (PhotoView) container.findViewWithTag(info);
            container.removeView(image);
        }
    }
}
